package com.java.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author jingjintao
 * @date 2021/6/18 - 22:13
 **/
public class ResponseIOServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器字符集为UTF-8
//        resp.setCharacterEncoding("UTF-8");
//        //通过响应头，设置浏览器也使用UTF-8字符集
//        resp.setHeader("Content-Type","text/html; charset=UTF-8");
//        System.out.println(resp.getCharacterEncoding());//默认为ISO-8859-1
        //它会同时设置服务器和客户端都使用UTF-8字符集，还设置了响应头
        //此方法一定要在获取流对象之前调用才有效
        resp.setContentType("text/html; charset=UTF-8");
        System.out.println(resp.getCharacterEncoding());
        //字节流和字符流不能同时使用，只能使用其中一个
        PrintWriter writer = resp.getWriter();
//        writer.write("response's content !!!");
        writer.write("我爱你");
    }
}
